from tinytag import TinyTag
from PyQt5.Qt import Qt
from PyQt5 import QtCore, QtGui, QtWidgets
import ui.resources_rc
from ui.Ui_message import Ui_Message

class ListButtonWidget(QtWidgets.QWidget):
    '''
    给表格列每行的按钮区新建一个类
    按钮按下发送对应行数信号，根据行数进行后续处理
    '''
    signal_play = QtCore.pyqtSignal(int)  # 播放信号
    signal_delItem = QtCore.pyqtSignal(int)  # 删除item信号

    def __init__(self, row):
        super(ListButtonWidget, self).__init__()

        self.setObjectName("widget")
        horizontalLayout_2 = QtWidgets.QHBoxLayout(self)
        horizontalLayout_2.setContentsMargins(-1, 0, -1, 0)
        horizontalLayout_2.setObjectName("horizontalLayout_2")
        pBtnPlay = QtWidgets.QPushButton(self)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(pBtnPlay.sizePolicy().hasHeightForWidth())
        pBtnPlay.setSizePolicy(sizePolicy)
        pBtnPlay.setStyleSheet("background-color: rgb(0, 170, 127);")
        pBtnPlay.setObjectName("pBtnPlay")
        horizontalLayout_2.addWidget(pBtnPlay)
        pBtnDelete = QtWidgets.QPushButton(self)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(pBtnDelete.sizePolicy().hasHeightForWidth())
        pBtnDelete.setSizePolicy(sizePolicy)
        pBtnDelete.setStyleSheet("background-color: rgb(255, 85, 0);")
        pBtnDelete.setObjectName("pBtnDelete")
        horizontalLayout_2.addWidget(pBtnDelete)
        pBtnPlay.setText("播放")
        pBtnDelete.setText("删除")

        self.row = row  # 按钮所对应的项目编号
        pBtnDelete.clicked.connect(self.deleteMusicEmit)
        pBtnPlay.clicked.connect(self.playMusicEmit)

    def deleteMusicEmit(self):
        self.signal_delItem.emit(self.row)  # 发送要删除的music的编号信号
    def playMusicEmit(self):
        self.signal_play.emit(self.row)  # 发送要播放的music的编号信号

class PicShowWidget(QtWidgets.QWidget):
    '''
    给缩略图中的每一项新建一个类
    删除按钮按下发送对应行数信号，根据行数进行后续处理
    点击这一项时会发送点击信号
    '''
    signalDelPicItem = QtCore.pyqtSignal(int)  # 删除item信号
    signalPlayPicItem = QtCore.pyqtSignal(int)  # 点击播放信号

    def __init__(self, row, music):
        super(PicShowWidget, self).__init__()

        self.setObjectName("PicModule")
        self.resize(316, 437)
        self.setStyleSheet("#Frame{\n"
"    background: rgb(85, 170, 255);\n"
"}\n"
"#widgetName,#widgetAuthor,#widgetTimeBtn{\n"
"    background: rgb(0, 127, 255);\n"
"}\n"
"#labelMusicName{\n"
"    font-family: \"Microsoft YaHei\";\n"
"    font-weight: bold;\n"
"    font-size: 14px;\n"
"    text-align: left;\n"
"    color: #ffffff;\n"
"}\n"
"#labelAuthor,#labelTime{\n"
"    font-family: \"Microsoft YaHei\";\n"
"    font-weight: 400;\n"
"    font-size: 12px;\n"
"    text-align: left;\n"
"    color: #ffffff;\n"
"}\n"
"QPushButton{\n"
"    font-family: \"Microsoft YaHei\";\n"
"    font-weight: 400;\n"
"    font-size: 14px;\n"
"    color: #ffffff;\n"
"    width:76px;\n"
"    height:28px;\n"
"    border: 0px;\n"
"}\n"
"#pBtnDelete{\n"
"    background-color: rgb(255, 85, 0);\n"
"}\n"
"#pBtnPlay{\n"
"    background-color: rgb(0, 170, 127);\n"
"}")
        horizontalLayout_3 = QtWidgets.QHBoxLayout(self)
        horizontalLayout_3.setContentsMargins(0, 0, 0, 0)
        horizontalLayout_3.setSpacing(0)
        horizontalLayout_3.setObjectName("horizontalLayout_3")
        spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Minimum)
        horizontalLayout_3.addItem(spacerItem)
        Frame = QtWidgets.QWidget(self)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(Frame.sizePolicy().hasHeightForWidth())
        Frame.setSizePolicy(sizePolicy)
        Frame.setObjectName("Frame")
        verticalLayout_2 = QtWidgets.QVBoxLayout(Frame)
        verticalLayout_2.setContentsMargins(10, 10, 10, 10)
        verticalLayout_2.setSpacing(0)
        verticalLayout_2.setObjectName("verticalLayout_2")
        labelPicture = QtWidgets.QLabel(Frame)
        labelPicture.setText("")

        labelPicture.setObjectName("labelPicture")
        verticalLayout_2.addWidget(labelPicture)
        widgetName = QtWidgets.QWidget(Frame)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Minimum)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(widgetName.sizePolicy().hasHeightForWidth())
        widgetName.setSizePolicy(sizePolicy)
        widgetName.setObjectName("widgetName")
        horizontalLayout = QtWidgets.QHBoxLayout(widgetName)
        horizontalLayout.setContentsMargins(12, 12, 12, 0)
        horizontalLayout.setObjectName("horizontalLayout")
        labelMusicName = QtWidgets.QLabel(widgetName)
        labelMusicName.setObjectName("labelMusicName")
        horizontalLayout.addWidget(labelMusicName)
        spacerItem1 = QtWidgets.QSpacerItem(10, 20, QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Minimum)
        horizontalLayout.addItem(spacerItem1)
        verticalLayout_2.addWidget(widgetName)
        widgetAuthor = QtWidgets.QWidget(Frame)
        widgetAuthor.setObjectName("widgetAuthor")
        horizontalLayout_4 = QtWidgets.QHBoxLayout(widgetAuthor)
        horizontalLayout_4.setContentsMargins(12, 4, 12, 4)
        horizontalLayout_4.setObjectName("horizontalLayout_4")
        labelAuthor = QtWidgets.QLabel(widgetAuthor)
        labelAuthor.setObjectName("labelAuthor")
        horizontalLayout_4.addWidget(labelAuthor)
        spacerItem2 = QtWidgets.QSpacerItem(10, 20, QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Minimum)
        horizontalLayout_4.addItem(spacerItem2)
        verticalLayout_2.addWidget(widgetAuthor)
        widgetTimeBtn = QtWidgets.QWidget(Frame)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Minimum)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(widgetTimeBtn.sizePolicy().hasHeightForWidth())
        widgetTimeBtn.setSizePolicy(sizePolicy)
        widgetTimeBtn.setObjectName("widgetTimeBtn")
        horizontalLayout_2 = QtWidgets.QHBoxLayout(widgetTimeBtn)
        horizontalLayout_2.setContentsMargins(12, 0, 12, 12)
        horizontalLayout_2.setObjectName("horizontalLayout_2")
        labelTime = QtWidgets.QLabel(widgetTimeBtn)
        labelTime.setObjectName("labelTime")
        horizontalLayout_2.addWidget(labelTime)
        spacerItem3 = QtWidgets.QSpacerItem(10, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        horizontalLayout_2.addItem(spacerItem3)
        pBtnPlay = QtWidgets.QPushButton(widgetTimeBtn)
        pBtnPlay.setObjectName("pBtnPlay")
        horizontalLayout_2.addWidget(pBtnPlay)
        pBtnDelete = QtWidgets.QPushButton(widgetTimeBtn)
        pBtnDelete.setObjectName("pBtnDelete")
        horizontalLayout_2.addWidget(pBtnDelete)
        verticalLayout_2.addWidget(widgetTimeBtn)
        horizontalLayout_3.addWidget(Frame)
        spacerItem4 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Minimum)
        horizontalLayout_3.addItem(spacerItem4)

        info=TinyTag.get(music,image=True)
        bytes=info.get_image()
        if bytes!=None:
            qpic=QtGui.QImage()
            qpic.loadFromData(bytes)
            qpicnew=qpic.scaled(220,220,transformMode=QtCore.Qt.SmoothTransformation)
            labelPicture.setPixmap(QtGui.QPixmap.fromImage(qpicnew))
        else:
            qpic=QtGui.QPixmap(":/resources/没有封面.jpg")
            qpicnew=qpic.scaled(220,220,transformMode=QtCore.Qt.SmoothTransformation)
            labelPicture.setPixmap(qpicnew)
        labelMusicName.setText(info.title if info.title!=None and info.title!='' else ''.join(music.split('/')[-1].split('.')[:-1]))
        labelAuthor.setText(info.artist if info.artist!=None and info.artist!='' else "未知歌手")
        m,s=divmod(info.duration,60)
        labelTime.setText("%02d:%02d" % (m,s))
        
        pBtnDelete.setText("删除")
        pBtnPlay.setText("播放")

        self.row = row  # 按钮所对应的项目编号
        pBtnDelete.clicked.connect(self.deletePicEmit)
        pBtnPlay.clicked.connect(self.playPicMusicEmic)

    def deletePicEmit(self):
        self.signalDelPicItem.emit(self.row)  # 发送要删除的music的编号信号
    def playPicMusicEmic(self):
        self.signalPlayPicItem.emit(self.row)
    def mousePressEvent(self, a0: QtGui.QMouseEvent) -> None:
        if a0.button() == Qt.LeftButton:
            self.signalPlayPicItem.emit(self.row)
        return super().mousePressEvent(a0)

class Message(QtWidgets.QMainWindow):
    signalchoosecancel=QtCore.pyqtSignal(int)
    signalchooseyes=QtCore.pyqtSignal(int)

    def __init__(self,row=None):
        super(Message, self).__init__()
        # 初始化窗口
        self.ui = Ui_Message()
        self.ui.setupUi(self)
        self.resize(300,200)
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.row=row
        self.ui.pBtnClose.clicked.connect(self.close)
        self.ui.pBtnCancel.clicked.connect(self.cancelbtnEmit)
        self.ui.pBtnOK.clicked.connect(self.yesbtnEmit)

    def cancelbtnEmit(self):
        self.signalchoosecancel.emit(self.row)
    def yesbtnEmit(self):
        self.signalchooseyes.emit(self.row)

menustylesheet='''
    QMenu{
        background-color: rgb(240, 255, 255);
        border: 0px;
        padding-left:10px;
    }
    QMenu::item{
        height: 40px;
        background-color: rgb(240, 255, 255);
        font-family: "Microsoft YaHei";
        font-weight: 400;
        font-size: 12px;
        color: #000000;
        padding-left:10px;
    }
    QMenu::item:selected{
        border: 0px;
        color: rgb(75, 144, 255);
    }
'''